<style>
    .liactive{
        background: #dcdcdc;
    }
</style>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">系统管理</a>
        <a><cite>组织结构</cite></a>
        <a><cite>组织结构管理</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">
                        组织结构管理
                    </div>
                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
                                    <button class="layui-btn ts-user-table-operate" data-type="add" lay-href="dept/add">
                                        <i class="layui-icon">&#xe654;</i>添加部门</button>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <div class="ts-user-table-btn" style="margin-bottom: 10px;">
                                    <label class="layui-form-label">部门名称</label>
                                    <div class="layui-inline">
                                        <input class="layui-input" type="text"  name="deptName" id="deptName" autocomplete="off">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <table class="layui-hide" id="ts-dept-table" lay-filter="ts-dept-table">
                        </table>
                        <script type="text/html" id="ts-dept-table-operate-bar">
                            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
                            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        </script>
                    </div>
                </div>
            </div>
    </div>
</div>
<script>
    layui.use(['admin','table','tree','format','common'], function () {
        var $ = layui.$,table = layui.table,admin=layui.admin,common=layui.common;

        /**
         * 初始化部门列表
         *
         * */
        var index = layer.load(2);
        table.render({
            elem: '#ts-dept-table'
            ,url: common.basePath+'system/tsDept/findTsUserPage.do'
            ,where: {
                access_token: layui.data('layuiAdmin').access_token
            },done: function(res){
                //alert('加载中');
                if(res.code == 1001){
                    layer.close(index);
                    layui.admin.exit();
                }
                layer.close(index);
            }
            ,cellMinWidth: 150
            ,limit: 10
            ,cols: [[
                {field:'deptId',checkbox: true, fixed: true }
                ,{field:'deptName', title: '部门名称',   sort: true, fixed: 'left',width: 150}
                ,{field:'deptCode', title: '部门编码',   sort: true, width: 150}
                ,{field:'parentName', title: '上级部门名称', sort: true,width: 200 }
                ,{field:'status', title: '状态状态', sort: true,width: 100
                    ,templet: function(tsDept){
                        var ret ='' ;
                        if(tsDept.status == 0){
                            ret = '正常'
                        }else{
                            ret = '停用'
                        }
                        return '<span>'+ ret +'</span>'
                    }
                }
                ,{field:'createTime', title: '创建时间', sort: true,width: 150
                    , templet:
                            function (tsDept){
                                return layui.format.UnixToDate(tsDept.createTime/1000,true);
                            }
                }
                ,{field:'creator', title: '创建人', sort: true,width: 150}
                ,{field:'modifyTime', title: '修改时间', sort: true,width: 150
                    , templet:
                            function (tsDept){
                                return layui.format.UnixToDate(tsDept.modifyTime/1000,true);
                            }
                }
                ,{field:'modifyer', title: '修改人', sort: true,width: 150}
                ,{width:178, align:'center', fixed: 'right', toolbar: '#ts-dept-table-operate-bar'}
            ]]
            ,page: true
            , height: 'full-300'
        });


        var active = {
            reload: function(){
                var deptName = $('#deptName');
                //执行重载
                table.reload('ts-dept-table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        access_token: layui.data('layuiAdmin').access_token,
                        // 新增搜索条件 ，进行搜索
                        deptName: deptName.val()
                    }
                });
            }
            ,imp: function(){
            },
            exp: function(){
            },
            lock: function(){
            }
        };

        /**
         * 监听工具条,单行的 删除修改 和 删除 ;
         */
        table.on('tool(ts-dept-table)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                location.hash = "/dept/detail/deptId="+ data.deptId  ;
            } else if(obj.event === 'del'){
                var array = [] ;
                array.push(data);
                layer.confirm('部门删除后不可恢复！', function(index){
                    layer.close(index);
                    var index_ajax = layer.load(2);
                    admin.req({
                        url:common.basePath + 'system/tsDept/delTsDept.do?tsDept='+JSON.stringify(array) //实际使用请改成服务端真实接口
                        ,type:'get'
                        ,done: function(res){
                            layer.close(index_ajax);
                            if(res.code == 0){
                                layer.msg(res.msg, {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                }, function(){
                                    active['reload'] ? active['reload'].call(this) : '';
                                });
                            }else {
                                layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                }, function () {
                                });
                                return ;
                            }
                        },success:function () {
                            layer.close(index_ajax);
                        },error:function () {
                            layer.close(index_ajax);
                        }});
                },function(){
                });
            } else if(obj.event === 'edit'){
                location.hash = "/dept/edit/deptId="+ data.deptId  ;
            }
        });

    });
</script>
